package org.test.springbootdisplaypage.controls;


import com.google.common.collect.ImmutableMap;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.test.springbootdisplaypage.device.Phione;

import java.time.Duration;


//用于处理接听语音相关的
@RestController
@RequestMapping("/AnswerVoice")
public class AnswerVoice {

//feature1：接听电话
    @RequestMapping("/answerPhone")
    public String answerPhone() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        //这里的等待时间重新设置一下，30秒内看不到元素就是失败的
        System.out.println("请在30秒内让对方拨打电话");
        WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(30));

        //等待通话页面的出现
        try{
            //等待来电-用接听按钮判断的
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioAnswerIncomingButton"))).click();
        }catch (TimeoutException e){
            //30秒钟还没弹出就是问题
            System.out.println("30秒钟没有弹出通话界面");
            driver.quit();
            return "30秒钟通话界面没有弹出";
        }

        Thread.sleep(5000);
        WebElement talkTime=driver.findElement(AppiumBy.accessibilityId("IndividualCallViewController.callStatusLabel"));
        if(talkTime.getAttribute("value").indexOf(':')!=-1){
            System.out.println("通话正常");
        }
        else{
            System.out.println("通话时间未出现，通话失败");
            driver.quit();
            return "通话时间未出现，通话失败";
        }
        System.out.println("已经成功接听电话");

        //点击挂断，结束本次通话
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioHangUpButton"))).click();
        Thread.sleep(3000);

        try{
            //还能检测到这个元素就不对了
            driver.findElement(AppiumBy.accessibilityId("IndividualCallViewController.audioHangUpButton"));
            driver.quit();
            return "挂断电话出现错误";
        }catch(NoSuchElementException e){
            System.out.println("已成功挂断电话，请观察");
        }

        driver.quit();
        return "已成功接听电话并挂断";
    }

//feature2:拒接来电
    @RequestMapping("/refuseAnswer")
    public String refuseAnswer() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        //这里的等待时间重新设置一下，30秒内看不到元素就是失败的
        System.out.println("请让对方在30秒内拨打电话");
        WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(30));

        //等待通话页面的出现
        try{
            //等待来电点击拒绝
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioDeclineIncomingButton"))).click();
        }catch (TimeoutException e){
            //30秒钟还没弹出就是问题
            System.out.println("30秒钟没有弹出通话界面");
            driver.quit();
            return "30秒钟通话界面没有弹出";
        }
        Thread.sleep(3000);

        try{
            //还能检测到这个元素就不对了
            driver.findElement(AppiumBy.accessibilityId("IndividualCallViewController.audioDeclineIncomingButton"));
            driver.quit();
            return "拒绝电话出现错误";
        }catch(NoSuchElementException e){
            System.out.println("已成功拒绝电话，请观察");
        }

        driver.quit();
        return "已拒绝来电，请观察";
    }


//feature3：等待自动挂断来电
    @RequestMapping("/timeoutHangUp")
    public String timeoutHangUp() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        //这里的等待时间重新设置一下，30秒内看不到元素就是失败的
        System.out.println("请让对方在30秒内拨打电话");
        WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(30));

        //等待通话页面的出现
        try{
            //等待来电页面出现-用拒绝按钮判断的
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioDeclineIncomingButton")));
        }catch (TimeoutException e){
            //30秒钟还没弹出就是问题
            System.out.println("30秒钟没有弹出通话界面");
            driver.quit();
            return "30秒钟通话界面没有弹出";
        }

        System.out.println("出现通话页面");
        Thread.sleep(64000);

        try{
            driver.findElement(AppiumBy.accessibilityId("IndividualCallViewController.audioDeclineIncomingButton"));
            System.out.println("超过60秒没有挂断");
            return "超过60秒没有挂断";
        }catch(NoSuchElementException e){
            System.out.println("已自动挂断");
        }

        driver.quit();
        return "已超时自动挂断，请观察";
    }


 //feature4：麦克风相机权限检测
    @RequestMapping("/microphoneCameraAccess")
    public String microphoneCameraAccess() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        // 进入设置应用
        driver.executeScript("mobile: launchApp", ImmutableMap.of("bundleId", "com.apple.Preferences"));

        while(driver.findElements(AppiumBy.accessibilityId("搜索")).size()==0){
            //向下滑动至搜索框出现
            driver.executeScript("mobile: swipe", ImmutableMap.of(
                    "direction", "down",      // 滑动方向：下滑
                    "percent", 0.5            // 滑动的距离百分比（相对于屏幕高度）
            ));
        }

        //定位搜索框
        WebElement search=driver.findElement(AppiumBy.accessibilityId("搜索"));
        search.click();
        search.sendKeys("zebra");

        //点击搜索出来的这个软件
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"Zebra Messenger\"`]"))).click();

        //点击之后进入权限设置页面，将软件的麦克风和相机权限关闭了
        WebElement microphone=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeSwitch[`name == \"麦克风\"`][2]")));
        WebElement Camera=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeSwitch[`name == \"相机\"`][2]")));

        //麦克风开着就关了
        if(microphone.getAttribute("value").equals("1")){
            microphone.click();
            System.out.println("麦克风权限已关闭");
        }

        //相机开着就关了
        if (Camera.getAttribute("value").equals("1")){
            Camera.click();
            System.out.println("相机权限已关闭");
        }

        //关闭设置应用
        driver.executeScript("mobile: terminateApp", ImmutableMap.of("bundleId", "com.apple.Preferences"));
        //设置完之后打开软件
        driver.executeScript("mobile: launchApp", ImmutableMap.of("bundleId", "com.zebra.messenger"));

        //请在30秒内拨打电话
        System.out.println("请让对方在30秒内拨打电话-1");
        wait=new WebDriverWait(driver, Duration.ofSeconds(30));

        //等待通话页面的出现
        try{
            //等待来电
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioAnswerIncomingButton")));
        }catch (TimeoutException e){
            //30秒钟还没弹出就是问题
            System.out.println("30秒钟没有弹出通话界面");
            driver.quit();
            return "30秒钟通话界面没有弹出";
        }
        WebElement answerButton=driver.findElement(AppiumBy.accessibilityId("IndividualCallViewController.audioAnswerIncomingButton"));
        answerButton.click();
        Thread.sleep(1000);

        //查看权限提示框是不是出现了
        try{
            //点击出现的权限提示中的设置
            driver.findElement(AppiumBy.accessibilityId("设置")).click();
        }catch(NoSuchElementException e){
            return "麦克风权限关闭后，麦克风仍然能够使用";
        }

        //进入权限的设置界面先给麦克风权限打开
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeSwitch[`name == \"麦克风\"`][2]"))).click();
        //关闭设置应用
        driver.executeScript("mobile: terminateApp", ImmutableMap.of("bundleId", "com.apple.Preferences"));
        //设置完之后打开软件
        driver.executeScript("mobile: launchApp", ImmutableMap.of("bundleId", "com.zebra.messenger"));

        //请在30秒内拨打电话
        System.out.println("请让对方挂断电话，且在30秒内再次拨打电话-2");
        wait=new WebDriverWait(driver, Duration.ofSeconds(30));

        //等待通话页面的出现
        try{
            //等待来电,这里能直接接听了
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioAnswerIncomingButton"))).click();
        }catch (TimeoutException e){
            //30秒钟还没弹出就是问题
            System.out.println("30秒钟没有弹出通话界面");
            driver.quit();
            return "30秒钟通话界面没有弹出";
        }
        Thread.sleep(2000);

        //到这里就需要点击摄像头了，但是相机权限是关闭的还是需要进行设置，尝试点击摄像头按钮
        try{
            //点击摄像机
            driver.findElement(AppiumBy.accessibilityId("IndividualCallViewController.audioModeVideoButton")).click();
            Thread.sleep(2000);

            //点击弹出的权限按钮
            driver.findElement(AppiumBy.accessibilityId("设置")).click();
        }catch(NoSuchElementException e){
            System.out.println("相机权限没有生效");
            return "相机权限没有生效";
        }

        //进入了设置界面，点击进行相机权限的设置
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeSwitch[`name == \"相机\"`][2]"))).click();
        //关闭设置应用
        driver.executeScript("mobile: terminateApp", ImmutableMap.of("bundleId", "com.apple.Preferences"));
        //设置完之后打开软件
        driver.executeScript("mobile: launchApp", ImmutableMap.of("bundleId", "com.zebra.messenger"));


        //重新拨打电话
        System.out.println("请让对方挂断电话，且在30秒内再次拨打电话-3");
        wait=new WebDriverWait(driver, Duration.ofSeconds(30));

        //等待通话页面的出现
        try{
            //等待来电,这里能直接接听了
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioAnswerIncomingButton"))).click();
        }catch (TimeoutException e){
            //30秒钟还没弹出就是问题
            System.out.println("30秒钟没有弹出通话界面");
            driver.quit();
            return "30秒钟通话界面没有弹出";
        }
        Thread.sleep(2000);

        //点击摄像机
        driver.findElement(AppiumBy.accessibilityId("IndividualCallViewController.audioModeVideoButton")).click();
        Thread.sleep(2000);

        //出现图像就是正确的
        if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name == \"IndividualCallViewController.localVideoView\"`]/XCUIElementTypeOther")).size()!=0){
            System.out.println("视频图像出现");
        }

        //点击拒绝结束本次通话
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.videoHangUpButton"))).click();

        driver.quit();
        return "相机、麦克风权限检测功能正常";
    }


//feature5:通话中使用麦克风（静音非静音）和摄像头功能检测+最小化功能检测
    @RequestMapping("/microphoneCameraMinimizationFeature")
    public String microphoneCameraMinimizationFeature() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait =new WebDriverWait(driver, Duration.ofSeconds(30));

        System.out.println("请让对方在30秒内拨打电话");

        //等待通话页面的出现
        try{
            //等待来电
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioAnswerIncomingButton"))).click();
        }catch (TimeoutException e){
            //30秒钟还没弹出就是问题
            System.out.println("30秒钟没有弹出通话界面");
            driver.quit();
            return "30秒钟通话界面没有弹出";
        }

        //点击静音按钮
        System.out.println("关闭麦克风");
        WebElement soundOff=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioModeMuteButton")));
        soundOff.click();//点击按钮进行静音

        //点击静音按钮之后进行判断，判断此元素的value是不是1，是1的时候就是成功打开了
        if(soundOff.getAttribute("value").equals("1")){
            System.out.println("麦克风已关闭，对话观察是否关闭。15秒内完成");
            Thread.sleep(15000);
        }
        else{
            System.out.println("麦克风关闭后按钮没有高亮");
            return "麦克风关闭后按钮没有高亮";
        }

        //点击视频按钮
        System.out.println("打开视频按钮");
        WebElement videoOn=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("IndividualCallViewController.audioModeVideoButton")));
        videoOn.click();
        System.out.println("视频已经打开，请在10秒内查看是否开启");
        Thread.sleep(10000);

        //点击视频后需要判断的有两个地方，一个是检查按钮是不是高亮了，另一个是检查视频图像是不是出现了
        try{
            //检查视频图像是否出现
            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name == \"IndividualCallViewController.localVideoView\"`]/XCUIElementTypeOther"));
            System.out.println("视频图像已出现");
        }
        catch(NoSuchElementException e){
            return "视频没有出现";
        }

        //判断视频按钮是不是高亮了
        try{
            driver.findElement(AppiumBy.accessibilityId("摄像头"));
            System.out.println("视频按钮已经成功高亮");
        }
        catch(NoSuchElementException e){
            return "视频按钮未高亮显示";
        }

        //点击返回看看最小化有没有出现
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();
        Thread.sleep(3000);

        //测试三个主界面有没有出现最小化横幅
        //聊天列表界面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"聊天\"`]"))).click();
        Thread.sleep(5000);
        try{
            //定位最小化横幅   如果没有视频界面，路径里面的第一个[]数字就得换成2，有视频的时候换成1
            WebElement minimizedBannerwait=driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther"));
            System.out.println("聊天界面最小化窗口正常出现");
        }
        catch(NoSuchElementException e){
            System.out.println("聊天界面最小化窗口没有正常出现");
            return "聊天界面最小化窗口没有正常出现";
        }

        //联系人界面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("联系人"))).click();
        try{
            //定位最小化横幅
            WebElement minimizedBannerwait=driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther"));
            System.out.println("联系人界面最小化窗口正常出现");
        }
        catch(NoSuchElementException e){
            System.out.println("联系人界面最小化窗口没有正常出现");
            return "联系人界面最小化窗口没有正常出现";
        }

        //设置界面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("设置"))).click();
        try{
            //定位最小化横幅
            WebElement minimizedBannerwait=driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther"));
            System.out.println("设置界面最小化窗口正常出现");
        }
        catch(NoSuchElementException e){
            System.out.println("设置界面最小化窗口没有正常出现");
            return "设置界面最小化窗口没有正常出现";
        }

        driver.quit();
        System.out.println("麦克风、视频按钮功能正常+通话页面最小化功能正常");
        return "麦克风、视频按钮功能正常+通话页面最小化功能正常";
    }

}
